<template>
  <div>
    <a-card :bordered="false">
      <f-search-form :more="false" @searchHandler="searchHandler">
        <f-search-form-item label="类型" name="formulaType" type="select" :data="api.formula.formulaType"/>
        <f-search-form-item label="标题" name="formulaTitle"/>
      </f-search-form>
      <f-table ref="table" rowKey="formulaId" :columns="columns" :multiple="true" :operator="operator" :params="queryParam" :api="api.formula">
        <span slot="status" slot-scope="text, record, index">
          <a-switch v-model="record.status==1" checkedChildren="是" unCheckedChildren="否" @change="(checked) => setStatus(checked,record)"/>
        </span>
        <span slot="formulaType" slot-scope="text, record, index">
          <a-tag color="pink" v-if="text==1">供货方返款</a-tag>
          <a-tag color="pink" v-if="text==2">利润公式</a-tag>
          <a-tag color="pink" v-if="text==3">结算公式</a-tag>
          <a-tag color="pink" v-if="text==5">开票税金</a-tag>
          <a-tag color="pink" v-if="text==4">供货方高开税金</a-tag>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="$refs.drawer.showDrawer(record)">编辑</a>
            <a-divider type="vertical"/>
          </template>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item v-if="$auth('table.delete')">
                <a href="javascript:;" @click="$refs.table.del(record)">删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </f-table>
      <formula-drawer ref="drawer"></formula-drawer>
    </a-card>
  </div>
</template>
<script>
  import formulaDrawer from './formula_drawer.vue'
  import api from './js/api'

  export default {
    name: 'TableList',
    components: {
      formulaDrawer
    },
    data() {
      let me = this
      //操作按钮
      let operator = []
      operator.push({
        name: '添加',
        icon: 'plus',
        that: this,
        handler: function(table, that) {
          that.$refs.drawer.showDrawer()
        }
      })
      operator.push({
        name: '删除',
        icon: 'delete',
        batch: true,
        handler: function(table) {
          table.remove()
        }
      })
      return {
        api: Object.assign(api, this.api),
        //列表初始化查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '#',
            width: '60',
            align: 'center',
            customRender: (text, record, index) => index + 1
          },
          {
            title: '标题',
            align: 'center',
            dataIndex: 'formulaTitle'
          },
          {
            title: '类型',
            align: 'center',
            dataIndex: 'formulaType',
            scopedSlots: { customRender: 'formulaType' }
          },
          {
            title: '公式内容',
            align: 'center',
            dataIndex: 'formulaContent'
          },
          {
            title: '状态',
            align: 'center',
            scopedSlots: { customRender: 'status' },
            dataIndex: 'status'
          },
          {
            title: '备注',
            align: 'center',
            dataIndex: 'remark'
          },
          {
            title: '操作',
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],
        operator: operator
      }
    },
    created() {
    },
    methods: {
      //设置状态
      setStatus(check, record) {
        this.http.post(this, {
          url: this.api.formula.setStatus,
          data: {
            ids: record.formulaId,
            status: Number(check)
          },
          success(me, data) {
            record.status = data
          }
        })
      }
    }
  }
</script>
